using System.Collections;
using System.IO;
using System.Xml.Serialization;
using SQLRunnerLib.Util;

namespace SQLRunnerLib.Util
{
	[XmlRoot(Namespace="", IsNullable=false)]
	public class RunnerSettings
	{
		[XmlElement()]
		public string defaultProperties = "";

		[XmlElement()]
		public string key = "";

		[XmlArrayItem("RunnerProperties", typeof(RunnerProperties))]
		public ArrayList Properties = new ArrayList();

		public RunnerProperties FindSetPropertiesByKeyField(string keyValue)
		{
			foreach (RunnerProperties properties in Properties)
			{
				if (properties.FindProperty(key).value.Equals(keyValue)) 
					return properties;
			}
			return null;
		}

		public RunnerProperties DefaultSetProperties()
		{
			return FindSetPropertiesByKeyField(defaultProperties);
		}

		public void Serialize(string fileName)
		{
			XmlSerializer aSerializer = new XmlSerializer(typeof(RunnerSettings));
			using(StreamWriter aWriter = new StreamWriter(fileName))
			{
				aSerializer.Serialize(aWriter, this);
				aWriter.Close();
			}
		}

		public static RunnerSettings Deserialize(string fileName)
		{
			RunnerSettings appSettings;
			XmlSerializer aSerializer = new XmlSerializer(typeof(RunnerSettings));
			using (FileStream aReader = new FileStream(fileName, FileMode.Open))
			{
				appSettings = (RunnerSettings) aSerializer.Deserialize(aReader);
				aReader.Close();
			}
			return appSettings;
		}

	}

}
